package cn.xinfei.xdecision.engine.runner.executor.strategy;

import cn.xinfei.xdecision.common.model.component.ComponentVo;
import cn.xinfei.xdecision.common.model.component.strategyout.StrategyOutput;
import cn.xinfei.xdecision.common.model.enginex.po.EngineNode;
import cn.xinfei.xdecision.common.utils.util.JsonUtils;
import cn.xinfei.xdecision.common.utils.util.runner.jeval.EvaluationException;
import cn.xinfei.xdecision.common.vo.FieldInfoVo;
import cn.xinfei.xdecision.engine.runner.context.PipelineContextHolder;
import cn.xinfei.xdecision.engine.runner.executor.ComponentHandler;
import cn.xinfei.xdecision.engine.runner.executor.NodeHandler;
import cn.xinfei.xdecision.engine.runner.executor.model.StateTransmitModel;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 引擎节点执行
 */
@Data
@Slf4j
public abstract class EngineComponentRunner extends EngineRunner<ComponentHandler> {

    public static final String PRE = "Component_";

    @Override
    public void before(ComponentHandler nodeHandler) {

    }

    @Override
    public void after(ComponentHandler nodeHandler, StateTransmitModel stateTransmitModel, Map<String, Object> nodeOutput, Map<String, Object> nodeInput) {
        Map<String, Object> ruleNodeOutput = stateTransmitModel.getNodeOutput();
        Map<String, Object> ruleNodeInput = stateTransmitModel.getNodeInput();

        ruleNodeOutput.putAll(nodeOutput);
        ruleNodeInput.putAll(nodeInput);

        if (PipelineContextHolder.isPersistence()==false) {
            log.info("测试数据不发起持久化");
            return;
        }
    }
    @Override
    public void clear() {

    }

    /**
     * 自定义输出配置里有可能存在变量伏值，该方法是从输出配置里提取出需要输入的 变量
     *
     * @param nodeHandler
     * @return
     */
    @Override
    public Map<String, FieldInfoVo> getInputFieldInfo(ComponentHandler nodeHandler) {
        Map<String, FieldInfoVo> in = new HashMap<>();
        return in;
    }

    @Override
    public Map<String, StrategyOutput> getTerminalOutputFieldInfo(ComponentHandler nodeHandler) {

        Map<String, StrategyOutput> in = new HashMap<>();
        return in;
    }

    @Override
    public Map<String, FieldInfoVo> getTerminalInputFieldInfo(NodeHandler nodeHandler) {
        Map<String, FieldInfoVo> in = new HashMap<>();
        return in;
    }

    @Override
    public Map<String, StrategyOutput> getOutputFieldInfo(ComponentHandler nodeHandler) {
        Map<String, StrategyOutput> outputResult = new HashMap<>();
        String nodeCode = nodeHandler.getCode();
        ComponentVo componentVo = nodeHandler.getComponentVo();
        Long versionNo = componentVo.getVersionNo();
        Long versionId = componentVo.getVersionId();
        try {

            String nodeType = nodeHandler.getNodeType();

            Map<String, FieldInfoVo> fieldInfo = metadataProvider.getFieldInfo();
            StrategyOutput strategyOutput = new StrategyOutput(versionId, nodeType);
            List<StrategyOutput> outputList = strategyOutputService.list(new QueryWrapper<>(strategyOutput));
            outputList.stream().forEach(out -> {
                FieldInfoVo fieldInfoVo = fieldInfo.get(out.getFieldCode());
                out.setVarValueType(fieldInfoVo.getValueType());
                out.setStage(fieldInfoVo.getStage());
                outputResult.put(out.getFieldCode(), out);
            });

        } catch (Exception e) {
            log.error("getOutputFieldInfo:获取指标异常,error={},nodeHandler={}", e.getMessage(), JsonUtils.toJsonString(nodeHandler), e);
        }
        return outputResult;
    }

    @Override
    public void getNodeFieldValue(ComponentHandler nodeHandler) {

    }

    @Override
    public boolean terminalCondition(ComponentHandler nodeHandler, Map<String, Object> nodeOutput,Map<String, Object> nodeInput) {
        return false;
    }



}
